**Housekeeping
clear all
cap log close
log using ${rep_root}/logs/robust.log, text replace
set more off



*******************
//INITIATE TABLE TO STORE RESULTS: ROBUSTNESS TABLE EACH FOR AFFIDAVIT AND SUSTAIN
//AFFIDAVIT TABLE WILL INCLUDE IV RESULTS AND SUSTAIN TABLE WILL INCLUDE REDUCED FORMS
*******************

foreach treatvar in treat_affidavit treat_sustain {
 




texdoc init ${rep_root}/output/tables/inv_sup_robust_`treatvar'.tex, replace force
tex \begin{tabular}{l*{4}{c}}
tex \hline\hline
tex                     &\multicolumn{1}{c}{Complaints}&\multicolumn{1}{c}{Arrests}&\multicolumn{1}{c}{Type 1 Arrests} \\
 
texdoc close

}


*******************
//RUN ROBUSTNESS FOR EACH OF THE CHECKS
*******************



foreach check in cutoff25 cutoff75 pooled { 


clear  


local cutoff = 50

if "`check'" == "cutoff25" {
	local cutoff = 25
	local varname ="Cutoff: 25"
}
if "`check'" == "cutoff75" {
	local cutoff = 75
	local varname ="Cutoff: 75"  
}
if "`check'" == "pooled" local varname ="Pooled LOM"  
 

 
local seed = 9999 
local case_lb = `cutoff'

local covarsmin d_mo_* d_dist_*
local covars vic_female_any vic_white_any acc_white acc_male acc_old cat_3 cat_4 d_mo_* d_dist_* arrests_pre complaints_pre force_pre any_prior_complaint tenure  
 
 
*****************
**BRING IN CASE STATUS + COMPLAINTS DATA AND APPLY DATA RESTRICTION
***************** 
 
do ${rep_root}/code/data_restrict.do

if "`check'" == "po_only"{
drop if acc_rank != "POLICE OFFICER"
}

*****************
**MERGE WITH OUTCOMES DATA
***************** 
merge 1:1 cr_id acc_id using ${rep_root}/data/outcomes, keep(3) nogen

 

*****************
**DEFINE TREATMENT AND MAKE COVARIATES
***************** 
 

**Drop investigators without enough cases
tab inv_id
sort inv_id 
by inv_id: gen inv_ct = _N
drop if inv_ct<`case_lb'
tab inv_id intake_yr

**Define some covariates
gen po = acc_rank == "POLICE OFFICER"
gen minor_inj = vic_inj_1<=1
gen vic_old = (year(inc_complaint_dt_1) - vic_byr_oldest)>=35 & !missing(vic_byr_oldest)
gen acc_old = acc_age>=35 & !missing(acc_age)
gen cat_3 = acc_cat==3
gen cat_4 = acc_cat==4
 
   
  

forvalues i = 1/25{
	gen d_dist_`i' = inc_district_1==`i'
}
forvalues yr = 2006/2008{
	gen d_yr_`yr' = intake_yr==`yr'

	forvalues m = 1/12{
		gen d_mo_yr_`m'_`yr' = intake_yr==`yr' & intake_mo==`m'
	}
	
}
forvalues yr = 2014/2018{
	gen d_yr_`yr' = intake_yr==`yr'
	
	forvalues m = 1/12{
		gen d_mo_yr_`m'_`yr' = intake_yr==`yr' & intake_mo==`m'
	}
}

gen d_noaff = finding_cd=="NO AFFIDAVIT"
gen d_unfound = finding_cd=="UNFOUNDED"
gen d_nosus = finding_cd=="NOT SUSTAINED"
gen d_exon = finding_cd=="EXONERATED"
gen d_sus = finding_cd=="SUSTAINED"
gen d_addlinv = finding_cd=="ADDITIONAL INVESTIGATION REQUESTED"

qui foreach var in vic_female_any vic_white_any acc_white acc_male acc_old cat_3 cat_4 arrests_pre complaints_pre force_pre any_prior_complaint tenure arrests_postc1_3 serious_postc1_3 complaints_postc1_3 arrests_postc4_12 serious_postc4_12 complaints_postc4_12 arrests_posty2 serious_posty2 complaints_posty2 d_noaff d_unfound d_nosus d_exon d_sus d_addlinv{
	sum `var'
	*local temp_m = r(mean)
	local ss_`var'_m : di %6.3f r(mean)
	*local temp_sd = r(sd)
	local ss_`var'_sd : di %6.3f r(sd)
}

**Define treatment
 

gen treat_affidavit = finding_cd != "NO AFFIDAVIT" 
gen treat_sustain = finding_cd == "SUSTAINED"   

gen era = inrange(intake_yr, 2006, 2009) 

foreach t in treat_affidavit treat_sustain{

**Create LOM
capture drop resid cr_resid cr_ct inv_tot_resid case inv_ct2  
qui reg `t' `covars'
predict resid, residuals

sort cr_id
by cr_id: egen cr_resid = total(resid)
by cr_id: gen cr_ct = _N

*sort inv_id intake_yr intake_mo
sort inv_id intake_yr
gen case = 1


if "`check'" != "pooled"{
by inv_id  intake_yr: egen inv_tot_resid = total(resid)
by inv_id  intake_yr: egen inv_ct2 = total(case)
}

if "`check'" == "pooled"{
sort inv_id era
by inv_id  era : egen inv_tot_resid = total(resid)
by inv_id era : egen inv_ct2 = total(case)
}

gen lom_`t' = (inv_tot_resid - cr_resid)/(inv_ct2-cr_ct)

 


sort inv_id
by inv_id: sum lom_`t'
}
 

 
 
 
**************************
**OUTPUT RESULTS FOR AFFIDAVIT; FOR SUSTAIN, JUST STORE THE MEAN AND OBS. COUNT
**************************

foreach treatvar in  treat_affidavit {  
 
 **FIRST STAGE
 
  
reg `treatvar' lom_`treatvar' `covars', cluster(inv_id)
local first = _b[lom_`treatvar']
local first_se = _se[lom_`treatvar']
local first_N = e(N)
boottest lom_`treatvar', weight(webb) nograph seed(`seed')
 
 
 local first_f = r(t)^2
local first_p = r(p)

local first : di %6.3f `first'
local first_f : di %6.3f `first_f'
 
local first : di %6.3f `first'
local first_se : di %6.3f `first_se'
local first_p : di %6.3f `first_p'
glo first_star = cond(`first_p' <0.01, "***", cond(`first_p' <0.05, "**", cond(`first_p' <0.1, "*", "")))
local first_f : di %6.3f `first_f'
 

 
**Create Tables

**SECOND STAGE
local varlist  complaints_postc1_3 complaints_postc4_12 complaints_posty2 arrests_postc1_3 arrests_postc4_12 arrests_posty2 type1_arrests_postc1_3 type1_arrests_postc4_12 type1_arrests_posty2 
foreach var of local varlist{
	
	
	
	//control for the other lom
	
	reg `var' lom_`treatvar' lom_treat_sustain `covars' , cluster(inv_id)
 
	local beta`var' = _b[lom_`treatvar']
 	
	boottest lom_`treatvar', weight(webb)  nograph seed(`seed')
 
local p`var' = r(p)
display r(p)


//get stars based on bootstrapped p values 
glo star_`var' = cond(`p`var'' <0.01, "***", cond(`p`var'' <0.05, "**", cond(`p`var'' <0.1, "*", "")))
local p`var' : di %6.3f `p`var'' 

matrix b`var' = e(b)
matrix v`var' = e(V)
scalar N= e(N)
local N`var' = N


local se`var' = sqrt(v`var'[1,1])

local beta`var' : di %6.3f `beta`var''
local se`var' : di %6.3f `se`var''
local N`var' : di %7.0fc `N`var''



sum `var'
local mean_`var' =r(mean)
local mean_`var' : di %6.2f `mean_`var''
}	 

if "`treatvar'" == "treat_affidavit" {
 
 
 
texdoc init ${rep_root}/output/tables/inv_sup_robust_`treatvar'.tex, append force  
tex \hline 
tex `varname' & `betacomplaints_postc1_3'${star_complaints_postc1_3}  & `betaarrests_postc1_3'${star_arrests_postc1_3}  & `betatype1_arrests_postc1_3'${star_type1_arrests_postc1_3}  \\
tex          & (`secomplaints_postc1_3')   &(`searrests_postc1_3')  &(`setype1_arrests_postc1_3')    \\
tex   & [`pcomplaints_postc1_3'] & [`parrests_postc1_3'] & [`ptype1_arrests_postc1_3']  \\ 
texdoc close
}
 
 

texdoc init ${rep_root}/output/tables/inv_sup_robust_`treatvar'.tex, append force
tex Dep. var. mean & `mean_complaints_postc1_3' & `mean_arrests_postc1_3' & `mean_type1_arrests_postc1_3'  \\
tex Observations & `Ncomplaints_postc1_3' &`Narrests_postc1_3' & `Ntype1_arrests_postc1_3'   \\   
//tex First stage& \multicolumn{3}{c}{`first'}   \\
//tex First stage F-stat & \multicolumn{3}{c}{`first_f'}   \\
texdoc close
}
 
}

********************
**CLOSE TABLES
********************

foreach treatvar in treat_affidavit{
  

texdoc init ${rep_root}/output/tables/inv_sup_robust_`treatvar'.tex, append force  
tex \hline \hline
tex \end{tabular}

texdoc close

}


 

log close
*clear
